//=============================================================================
// Portal.
//=============================================================================

class TFPortal extends TFStation
	placeable;

replication
{
	reliable if ( Role < ROLE_Authority )
		Teleportation;
}

function bool Teleportation(pawn p, tfstation origin)
{
    local vector dest/*, newdest*/;
    local rotator rot;
    local vector HitLocation, HitNormal;
    local float overdist;

    dest=location+vector(rotation)*45;
    overdist=fmax(0,vsize(location-origin.location)-tfgame(level.Game).OutpostDist*72);

    if((origin.grade==3 || (origin.grade==2 && (overdist==0 || nearOutpost>0)))
        && Trace(HitLocation,HitNormal,Location,dest,true)==none
        && p.SetLocation(dest))
    {
        rot=p.Rotation;
        rot.yaw=rotation.Yaw;
        p.SetRotation(Rot);
    	p.ClientSetRotation(Rot);

        rot=p.getviewRotation();
        rot.yaw=rotation.Yaw;
        p.SetViewRotation(Rot);

        playercontroller(p.Controller).ClientFlash(0.1, vect(1500,0,2000));
        spawn(class'Taskforces.tfPortalOutFX',,,dest);
        PlaySound(Sound'TFMedia.tfTeleOut',SLOT_None,5,,,,false);
        origin.PlayOwnedSound(Sound'TFMedia.tfTeleIn',SLOT_None,5,,,,false);
        origin.spawn(class'Taskforces.tfPortalInFX',,,origin.location-vector(origin.Rotation)*40);
        return true;
    }

    if(overdist>0) Playercontroller(p.controller).ReceiveLocalizedMessage(class'TaskForces.tfPortalFailMsg',overdist+1.1);
    else Playercontroller(p.controller).ReceiveLocalizedMessage(class'TaskForces.tfPortalFailMsg');
    Playercontroller(p.controller).ClientPlaySound(Sound'TFMedia.TFGuiFail');
    return false;
}

state Ready
{
    function BeginState()
    {
        super.BeginState();

        if(myteam==0) setstaticmesh(StaticMesh'TFMedia.Buildings.tfPortalRed');
        else setstaticmesh(StaticMesh'TFMedia.Buildings.tfPortalBlu');
    }

    function Timer()
    {
    }
}

defaultproperties
{
     MaxHealth=1000.000000
     chunkcount=5
     BuildingName="Portal"
     NameIndex=3
     StaticMesh=StaticMesh'TFMedia.Buildings.tfPortal'
     AmbientSound=Sound'TFMedia.Warpblade.tfWBShifted'
     SoundPitch=48
}
